package top.weiliang.springboot3.controller.message;

import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.weiliang.springboot3.annotation.Limit;
import top.weiliang.springboot3.base.PageBean;
import top.weiliang.springboot3.base.R;
import top.weiliang.springboot3.entity.message.TbMessage;
import top.weiliang.springboot3.service.message.MessageService;
import top.weiliang.springboot3.vo.PageVO;
import top.weiliang.springboot3.volidate.CommonValidate;

@RestController
@RequestMapping("/message")
@RequiredArgsConstructor
public class MessageController {
    private final MessageService tbMessageService;

    @Limit(key = "/message/insert" ,message = "不要频繁发送")
    @RequestMapping("")
    public R<String> insert(@RequestBody @Validated(CommonValidate.Add.class) TbMessage message){
        return tbMessageService.insert(message);
    }

        @GetMapping("/{ps}/{pn}")
    public R<PageVO<TbMessage>> query(TbMessage tbMessage
    , @PathVariable("ps") long ps, @PathVariable("pn") long pn) {
        return tbMessageService.query(tbMessage, PageBean.instance(pn,ps));
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") Integer id){
        return tbMessageService.delete(id);
    }

    @PutMapping("")
    public R<String> update(@RequestBody @Validated(CommonValidate.Edit.class) TbMessage tbmessage){
        return tbMessageService.update(tbmessage);
    }
}
